<template>
	<view class="">
		<view class="bt-block m-3 p-2">
			<u--form errorType="toast" labelWidth="80" :labelStyle="{ fontSize: '28rpx' }" labelPosition="left"
				:model="postData" :rules="rules" ref="uForm">
				<u-form-item required label="项目名称" prop="projectId">
					<view class="form-block">
						<bg-select :value.sync="postData.projectId" :columns="projectList" key="projectId"></bg-select>
					</view>
				</u-form-item>
				<view class="bt-line-1"> </view>
				<u-form-item labelPosition="left" required label="选择人员" prop="userId">
					<view class="text-right flex-1">{{ postData.userName }}</view>
				</u-form-item>
				<view class="mb-3" @tap="selectUser">
					<u--image width="152rpx" height="152rpx" src="/static/images/ui/edu/select-user.png"></u--image>
				</view>
				<!-- <view class="bt-line-1"> </view>
				<u-form-item labelWidth="168" labelPosition="top" required label="上传三级教育(最少1张)" prop="imgs">
				</u-form-item>
				<view>
					<bg-upload key="imgs" :value.sync="postData.imgs" :count="9" :propPic="postData.imgsList"
						:propValue="postData.imgsProp"></bg-upload>
				</view> -->
				<view class="bt-line-1"> </view>
				<u-form-item labelWidth="220" labelPosition="top" required label="班组管理员-班组级安全教育(最少1张)" prop="teamImgs">
				</u-form-item>
				<view>
					<bg-upload key="teamImgs" :value.sync="postData.teamImgs" :count="9" :propPic="postData.teamImgsList"
						:propValue="postData.teamImgsProp"></bg-upload>
				</view>
				<view class="bt-line-1"> </view>
				<u-form-item labelWidth="220" labelPosition="top" required label="项目管理员-项目级安全教育(最少1张)" prop="projectImgs">
				</u-form-item>
				<view>
					<bg-upload key="projectImgs" :value.sync="postData.projectImgs" :count="9" :propPic="postData.projectImgsList"
						:propValue="postData.projectImgsProp"></bg-upload>
				</view>
				<view class="bt-line-1"> </view>
				<u-form-item labelWidth="220" labelPosition="top" required label="安全管理员-公司级安全教育(最少1张)" prop="safetyImgs">
				</u-form-item>
				<view>
					<bg-upload key="safetyImgs" :value.sync="postData.safetyImgs" :count="9" :propPic="postData.safetyImgsList"
						:propValue="postData.safetyImgsProp"></bg-upload>
				</view>
			</u--form>

		</view>
		<view class="bt-footer">
			<bg-btn @click="saveData">提交</bg-btn>
		</view>
	</view>
</template>

<script>
import { threeSave, threeDetail, threeEdit } from "@/api/system/study.js"

export default {
	onLoad(options) {
		this.id = options.id
		if (this.id) {
			this.getData()
		}
	},
	mounted() {
		this.$store.dispatch("types/getProjects").then(res => {
			this.projectList = res
		})
		uni.$on("selected-user", (res) => {
			this.postData.userId = res.userId
			this.postData.userName = res.userName
		})
	},
	data() {
		return {
			id: '',
			projectList: [],
			postData: { projectId: '', userId: '', userName: '',
			 // imgs: '' ,
			 },
			rules: {
				'projectId': {
					type: 'number',
					required: true,
					message: '请选择项目',
					trigger: ['blur', 'change']
				},
				'imgs': {
					required: true,
					message: '请上传三级教育',
					trigger: ['blur', 'change']
				},
				'userId': {
					type: 'number',
					required: true,
					message: '请选择人员',
					trigger: ['blur', 'change']
				},
			}
		}
	},
	methods: {
		getData() {
			threeDetail(this.id).then(res => {
				this.postData = res.data
				this.postData.imgsProp = this.postData.imgs
				this.postData.teamImgsProp = this.postData.teamImgs
				this.postData.projectImgsProp = this.postData.projectImgs
				this.postData.safetyImgsProp = this.postData.safetyImgs

			})
		},
		selectUser() {
			if (this.postData.projectId) {
				uni.navigateTo({
					url: "/pages/common/user-select/user-selector?pid=" + this.postData.projectId
				})
			} else {
				this.$modal.msg("请先选择项目")
			}
		},
		saveData() {
			if (this.$auth.hasRoleOr(['super', 'fanance'])) return this.$modal.msgSuccess("暂无提交权限")
			// console.log('this.postData----------', this.postData);
			// return
			this.$refs.uForm.validate().then(res => {
				let method = null;
				if (this.postData.id) {
					method = threeEdit
				} else {
					method = threeSave
				}
				method(this.postData).then(res => {
					this.$modal.msgSuccess("提交成功")
					uni.$emit("refresh-three", true)
					setTimeout(() => {
						uni.navigateBack()
					}, 500)
				})
			})
		}
	}
}
</script>

<style></style>